package com.atguigu.admin.service.impl;

import com.atguigu.admin.service.ProductService;
import com.atguigu.clients.ProductClient;
import com.atguigu.param.ProductSaveParam;
import com.atguigu.param.ProductSearchParam;
import com.atguigu.pojo.Product;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductClient productClient;

    @Cacheable(value = "list.product",key = "#productSearchParam.currentPage+'-'+#productSearchParam.pageSize")
    @Override
    public R list(ProductSearchParam productSearchParam) {

        return productClient.search(productSearchParam);
    }

    @CacheEvict(value = "list.product",allEntries = true)
//    @Cacheable(value = "product",key = "#productSaveParam.productId") 错误，因为#productSaveParam.productId为null
    @Override
    public R save(ProductSaveParam productSaveParam) {

        return productClient.save(productSaveParam);
    }


    @CacheEvict(value = "list.product",allEntries = true)
    @CachePut(value = "product",key = "#product.productId")
    @Override
    public R update(Product product) {

        return productClient.update(product);
    }

    @Caching(
            evict = {
                    @CacheEvict(value = "list.product",allEntries = true),
                    @CacheEvict(value = "product",key = "#productId")
            }
    )
    @Override
    public R remove(Integer productId) {

        return productClient.remove(productId);
    }
}
